package com.neusoft.dao.impl;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.neusoft.dao.AdminDao;
import com.neusoft.dao.BaseDao;
import com.neusoft.model.Admin;
import org.junit.Test;

/*	Dao类主要跟数据库打交道
 * 		负责数据库的增删改查
 * 	JavaBean
 * 		一般与数据库表结构一致，一般放在model或entity包中
 */
public class AdminDaoImpl extends BaseDao<Admin> implements AdminDao {

	@Override
	public boolean insert(Admin bean) {
		String sql = "insert into ums_admin(id, username, password, icon, email, nick_name, note, create_time, login_time, status) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		return doIUD(sql, bean.getId(), bean.getUsername(), bean.getPassword(), bean.getIcon(), bean.getEmail(),
				bean.getNickName(), bean.getNote(), bean.getCreateTime(), bean.getLoginTime(), bean.getStatus());
	}

	@Override
	public boolean update(Admin bean) {
		String sql = "update ums_admin set password = ?, status = ?, create_time = ? where id = ?";
		return doIUD(sql, bean.getPassword(), bean.getStatus(), bean.getCreateTime(), bean.getId());
	}

	@Override
	public boolean delete(int id) {
		String sql = "delete from ums_admin where id = ?";
		return doIUD(sql, id);
	}
	
	/*	查询方法
	 * 		1、查询多条
	 * 		2、查询单条
	 */
	@Override
	public Admin getById(Integer id) {
		String sql = "select * from ums_admin where id = ?";

		return query(sql, Admin.class, null, id);
	}

	@Override
	public Admin getByUsernameAndPassword(String username, String password) {
		String sql = "select * from ums_admin where username=? and password=?";

		return query(sql, Admin.class, null, username, password);
	}

	@Override
	public List<Admin> getList() {
		String sql = "select * from ums_admin";
		List<Admin> list = new ArrayList<>();
		query(sql, Admin.class, list);

		return list;
	}

	@Override
	public List<Admin> getListByUsername(String username) {
		String sql = "select * from ums_admin where username like ?";
		List<Admin> list = new ArrayList<>();
		query(sql, Admin.class, list, username);

		return list;
	}

}
